package com.xdxc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xdxc.mapper.BankQuestionMapper;
import com.xdxc.model.entity.BankQuestion;
import com.xdxc.model.entity.QuestionBank;
import com.xdxc.response.Result;
import com.xdxc.service.BankQuestionService;
import com.xdxc.vo.QuestionWithOptionsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/bank-questions")
public class BankQuestionController {

    @Autowired
    private BankQuestionService bankQuestionService;
    @Autowired

    private BankQuestionMapper bankQuestionMapper;

    @GetMapping
    public List<BankQuestion> getAll() {
        return bankQuestionService.list();
    }

    @PostMapping
    public boolean add(@RequestBody BankQuestion bankQuestion) {
        return bankQuestionService.save(bankQuestion);
    }
    @PostMapping("/batch")
    public Result addBatch(@RequestBody List<BankQuestion> bankQuestions) {

        if (bankQuestions==null){
            Result.error("").setMsg("添加题目失败");
        }
        boolean   result= bankQuestionMapper.batchInsertIgnoreDuplicate(bankQuestions);
        if (result){
            Result.error("").setMsg("添加题目失败");
        }
        return Result.success("").setMsg("添加题目成功");
    }
    @GetMapping("/{id}")
    public BankQuestion getById(@PathVariable Long id) {
        return bankQuestionService.getById(id);
    }

    @PutMapping
    public boolean update(@RequestBody BankQuestion bankQuestion) {
        return bankQuestionService.updateById(bankQuestion);
    }

    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Long id) {
        return bankQuestionService.removeById(id);
    }

    @GetMapping("/by-bank")
    public List<BankQuestion> getByBankId(@RequestParam Long bankId) {
        return bankQuestionService.query().eq("bank_id", bankId).list();
    }

    @GetMapping("/by-question")
    public List<BankQuestion> getByQuestionId(@RequestParam Long questionId) {
        return bankQuestionService.query().eq("question_id", questionId).list();
    }


    @GetMapping("/pageBankQuestion")
    public Result bankQuestionPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize,Long bankId) {
        return   bankQuestionService.BankQuestionPage(pageNum,pageSize,bankId);
    }

}